package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.common.ImageDishObject;
import vn.com.ltv.lo.dtos.ImageDishDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ImageDishDAO {
	protected static final String TABLE_NAME = "Image_dish";
	protected static final String ID = "rowid";
	protected static final String IMG_DISH_ID = "img_dish_id";
	protected static final String DSH_ID = "dsh_id";
	protected static final String IMG_NM = "img_nm";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ IMG_DISH_ID + " text, " + DSH_ID + " text, " + IMG_NM + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public ImageDishDAO(Context context) {
		this.context = context;
	}

	public ArrayList<ImageDishDTO> getAll() {
		ArrayList<ImageDishDTO> imageDiskDTOs = new ArrayList<ImageDishDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				ImageDishDTO imageDiskDTO = this.cursorToImageDiskDTO(cursor);
				imageDiskDTOs.add(imageDiskDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return imageDiskDTOs;
	}

	public ImageDishDTO get(int id) {
		ImageDishDTO imageDiskDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			imageDiskDTO = this.cursorToImageDiskDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return imageDiskDTO;
	}
	
	public ArrayList<ImageDishObject> getListImageBySetId(String id) {
		ArrayList<ImageDishObject> listImages = new ArrayList<ImageDishObject>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		String query = "SELECT img_pth, id.dsh_id, id.img_id FROM Image_Dish as id, Image as i, Set_List sl WHERE id.img_id = i.img_id AND sl.dsh_id = id.dsh_id AND sl.set_id = '"+id+"'";
		Cursor cursor = sQLiteDatabase.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				ImageDishObject imageDishObject = new ImageDishObject();
				String imgDishPath = cursor.getString(cursor.getColumnIndex("img_pth"));
				imageDishObject.setPath(imgDishPath);
				String imgImgId = cursor.getString(cursor.getColumnIndex("img_id"));
				imageDishObject.setImg_id(imgImgId);
				String imgDshId = cursor.getString(cursor.getColumnIndex("dsh_id"));
				imageDishObject.setDsh_id(imgDshId);
				listImages.add(imageDishObject);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return listImages;
	}

	public ArrayList<ImageDishObject> getListImageByDishId(String id) {
		ArrayList<ImageDishObject> listImages = new ArrayList<ImageDishObject>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		String query = "SELECT img_pth, id.dsh_id, id.img_id FROM Image_Dish as id, Image as i WHERE id.img_id = i.img_id AND id.dsh_id = '"+id+"'";
		Cursor cursor = sQLiteDatabase.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				ImageDishObject imageDishObject = new ImageDishObject();
				String imgDishPath = cursor.getString(cursor.getColumnIndex("img_pth"));
				imageDishObject.setPath(imgDishPath);
				String imgImgId = cursor.getString(cursor.getColumnIndex("img_id"));
				imageDishObject.setImg_id(imgImgId);
				String imgDshId = cursor.getString(cursor.getColumnIndex("dsh_id"));
				imageDishObject.setDsh_id(imgDshId);
				listImages.add(imageDishObject);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return listImages;
	}
	
	public void insert(ImageDishDTO imageDiskDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this
				.imageDiskDTOToContentValues(imageDiskDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(ImageDishDTO imageDiskDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = imageDiskDTO.getId();
		imageDiskDTO.setId(-1);
		ContentValues contentValues = this
				.imageDiskDTOToContentValues(imageDiskDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private ImageDishDTO cursorToImageDiskDTO(Cursor cursor) {
		ImageDishDTO imageDiskDTO = new ImageDishDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		imageDiskDTO.setId(id);
		String imgDishId = cursor.getString(cursor.getColumnIndex(IMG_DISH_ID));
		imageDiskDTO.setImgDishId(imgDishId);
		String dshId = cursor.getString(cursor.getColumnIndex(DSH_ID));
		imageDiskDTO.setDshId(dshId);
		String imgNm = cursor.getString(cursor.getColumnIndex(IMG_NM));
		imageDiskDTO.setImgNm(imgNm);
		return imageDiskDTO;
	}

	private ContentValues imageDiskDTOToContentValues(ImageDishDTO imageDiskDTO) {
		ContentValues contentValues = new ContentValues();
		int id = imageDiskDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String imgDishId = imageDiskDTO.getImgDishId();
		if (imgDishId != null) {
			contentValues.put(IMG_DISH_ID, imgDishId);
		}
		String dshId = imageDiskDTO.getDshId();
		if (dshId != null) {
			contentValues.put(DSH_ID, dshId);
		}
		String imgNm = imageDiskDTO.getImgNm();
		if (imgNm != null) {
			contentValues.put(IMG_NM, imgNm);
		}
		return contentValues;
	}
}